-- 属性tips

local attrNodeNum = 7 -- 属性条目数
local attrNodeHeight = 0 -- 属性条目高度

local disToBoundary = 0 -- 上下边距

local newEquipPropTipsView = class("newEquipPropTipsView",function()
    return ccui.Layout:create()
end)

function newEquipPropTipsView:ctor()
    local function createModuleNode(uiPath, uiNodeName, parent)
        local node = cc.uiloader:load( uiPath )
        local moduleNode = node:getChildByName( uiNodeName )
        moduleNode:retain()
        moduleNode:removeFromParent()
        parent:addChild( moduleNode )
        moduleNode:release()
        return moduleNode
    end
    self.mUINode = createModuleNode( Res.Tips_WeaponExProp, "Tips_2", self )
    local muiNodeWidth, muiNodeHeight = self.mUINode:getContentSize2()
    self:setContentSize( cc.size( muiNodeWidth, muiNodeHeight ) )
    ccui.Helper:doLayout( self.mUINode )

    local node_1_posY = self.mUINode:getChildByName( "node_1" ):getPositionY()
    local _, attrRenderHeight = self.mUINode:findChild( "node_1/Panel" ):getContentSize2()
    disToBoundary = muiNodeHeight - ( node_1_posY + attrRenderHeight )

    self._line = self.mUINode:getChildByName( "line" )
    self._attrNodeList = {}
    self._attrNodePanelTltList = {}
    self._attrNodePanelNumList = {}
    for i = 1, attrNodeNum do
        local nodeTemp = self.mUINode:getChildByName( "node_" .. i )
        nodeTemp:setVisible( false )
        table.insert( self._attrNodeList, nodeTemp )

        local nodePanelTemp = nodeTemp:getChildByName( "Panel" )
        _, attrNodeHeight = nodePanelTemp:getContentSize2()

        local nodePanelTltTemp = nodePanelTemp:getChildByName( "data_tlt" )
        table.insert( self._attrNodePanelTltList, nodePanelTltTemp )

        local nodePanelNumTemp = nodePanelTemp:getChildByName( "data_num" )
        table.insert( self._attrNodePanelNumList, nodePanelNumTemp )
    end
end

-- 帮助函数：设置一条属性的具体文本
function newEquipPropTipsView:setTextnodeStr( attrIdx, attrName, attrNum )
    self._attrNodePanelTltList[ attrIdx ]:setString( attrName or "" )
    self._attrNodePanelNumList[ attrIdx ]:setString( attrNum or "" )

    local width = self._attrNodePanelTltList[ attrIdx ]:getContentSize2()
    local posX = self._attrNodePanelTltList[ attrIdx ]:getPositionX()
    self._attrNodePanelNumList[ attrIdx ]:setPositionX( posX + width )
end

-- { { propNameStr, propNumStr, attrColor }, { propNameStr, propNumStr, attrColor },,,}
function newEquipPropTipsView:setEquipAttrTipsData( shownPropList ) -- 显示属性列表
    for i = 1, attrNodeNum do
        self._attrNodeList[ i ]:setVisible( false )
    end

    for i,v in ipairs( shownPropList ) do
        if self._attrNodeList[ i ] then
            self._attrNodeList[ i ]:setVisible(true)
            self:setTextnodeStr( i, v[1], v[2] )

            if v[3] then
                self._attrNodePanelTltList[ i ]:setTextColor( v[3] )
                self._attrNodePanelNumList[ i ]:setTextColor( v[3] )
            else
                self._attrNodePanelTltList[ i ]:setTextColor( cc.c3b( 183, 183, 183 ) )
                self._attrNodePanelNumList[ i ]:setTextColor( cc.c3b( 255, 225, 132 ) )
            end
        end
    end

    -- 以下：调整高度
    local totalHeight = #shownPropList*attrNodeHeight + disToBoundary*2
    local muiNodeWidth = self.mUINode:getContentSize2()
    self.mUINode:setContentSize( cc.size( muiNodeWidth, totalHeight ) )
    self._line:setPositionY( totalHeight )
    -- 调整itemY坐标位置
    for i = 1, attrNodeNum do
        local tempPosY = totalHeight - disToBoundary - attrNodeHeight * i
        self._attrNodeList[i]:setPositionY( tempPosY )
    end

    self:setContentSize( self.mUINode:getContentSize() )
    ccui.Helper:doLayout( self.mUINode )
end

return newEquipPropTipsView
